// pages/cmdView/ecg/ecg.js

const util = require("../../../utils/util");
const appControlAPI = requirePlugin("hello-plugin")
const alertUtil = require("../../../utils/alertUtil")
const {

  // API的状态码
  succeed,
  
  YCCollectDataKeyReceiveCheckInfo,
  // 控制类型
  realBloodPressureUpload,
  realHrvUpload,

  // MARK: - 实时数据上传类型
  queryHealthDataECG,
  receivedRealTimeBloodPressure,
  receivedRealTimePPG,
  receivedRealTimeECG,
  receivedRealTimePressure,
  electrodeKey,

  // MARK: - 波形上传控制
  waveUploadStateUploadWithOutSerialnumber,
  waveUploadDataTypePPG,
  waveUploadDataTypeECG,

    // 测量控制
    continuous,

    // ecg测量状态
    measureECGStatus,

} = require("../../../utils/constant")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    canvasText: "接收到设备信息：",
    ecgCount: 0,
    ecgList: [],
    isUpload: true,   // 是否上报
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
/**
 * 获取ECG数据总数
 */
  queryEcgCount(){
    appControlAPI.queryCollectDataCount( queryHealthDataECG ,(state, res) =>  {
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：" + JSON.stringify(res),
          ecgCount:  res
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据："+ JSON.stringify(res)
        })
      }
      alertUtil.closeToast()
    })
  },
  /**
   * 根据索引获取ECG数据
   */
  queryEcgList(){
    this.setData({
      canvasText: this.data.canvasText + "\n返回"+this.data.ecgCount+"条数据："
    })
    for (let index = 0; index < this.data.ecgCount; index++) {
      appControlAPI.receiveRealTimeData( (realTimeDataType, res) => {
        if (realTimeDataType == YCCollectDataKeyReceiveCheckInfo) {
          this.setData({
            canvasText: this.data.canvasText + "\n"+ JSON.stringify(res),
          })
        }
      })
      appControlAPI.queryCollectIndexDataInfo(queryHealthDataECG,index,this.data.isUpload,(state,res)=>{
        if (this.data.isUpload) {
        } else {
          if (state == succeed) {
            this.setData({
              canvasText: this.data.canvasText + "\n"+ JSON.stringify(res),
              ecgList:[...this.data.ecgList, res]
            })
          } else{
            this.setData({
              canvasText: this.data.canvasText + "\n返回数据："+ JSON.stringify(res),
            })
          }
        }
        alertUtil.closeToast()
      })

    }
  },
  /**
   * 根据索引删除ECG数据
   */
  deleteEcgForIndex(){
    appControlAPI.deleteCollectIndexData(queryHealthDataECG, this.data.ecgCount-1 ,(state,res)=>{
      if (state == succeed) {
        // 删除数据，去除存储数据最后一条
          this.data.ecgList.pop(); 
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：success" ,
          ecgCount: this.data.ecgCount - 1 
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：fail"
        })
      }
      alertUtil.closeToast()
    })
  },
  /**
   * 根据时间戳查询ECG数据
   */
  queryEcgForTimeStamp(){
    console.log("获取数据ecgList:",this.data.ecgList);

    this.setData({
      canvasText: this.data.canvasText + "\n返回"+this.data.ecgList.length+"条数据："
    })
    for (let index = 0; index < this.data.ecgList.length; index++) {
      appControlAPI.queryCollectStampDataTimeInfo(queryHealthDataECG,this.data.ecgList[index].timeStamp,false,(state,res)=>{
        if (state == succeed) {
          this.setData({
            canvasText: this.data.canvasText + "\n"+ JSON.stringify(res),
          })
        } else{
          this.setData({
            canvasText: this.data.canvasText + "\n返回数据："+ JSON.stringify(res),
          })
        }
        alertUtil.closeToast()
      })
    }
  },
  /**
   * 根据时间戳删除ECG数据
   */
  deleteEcgForTimeStamp(){
    console.log("获取数据ecgList:",this.data.ecgList);
    appControlAPI.deleteCollectTimeStampData(queryHealthDataECG, this.data.ecgList[this.data.ecgCount-1].timeStamp ,(state,res)=>{
      if (state == succeed) {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：success" ,
          ecgCount: this.data.ecgCount - 1 
        })
      } else {
        this.setData({
          canvasText: this.data.canvasText + "\n返回数据：fail"
        })
      }
      alertUtil.closeToast()
    })
  },

  // 开始测量ECG
  startECG(){
    // 开启血压检测
    appControlAPI.bloodPressureMeasureController( continuous, (status,res)=>{
      if(status == succeed){
        this.setData({
          canvasText: this.data.canvasText + "\n开启血压监测：success"
        })
        // 开启实时ECG上传
          appControlAPI.waveDataUploadControl(waveUploadStateUploadWithOutSerialnumber, waveUploadDataTypeECG, (state, res) => {
            if (state == succeed) {
              this.setData({
                canvasText: this.data.canvasText + "\n开启ECG上传：success"
              })
              // 开启实时获取血压数据
              appControlAPI.realTimeDataUplod(true, realBloodPressureUpload, (state, res) => {
                if (state == succeed) {
                  this.setData({
                    canvasText: this.data.canvasText + "\n开启实时获取血压：success"
                  })
                } else {
                  this.setData({
                    canvasText: this.data.canvasText + "\n开启实时获取血压：fail"
                  })
                }
              })
              // 开启实时获取hrv数据
              appControlAPI.realTimeDataUplod(true, realHrvUpload, (state, res) => {
                if (state == succeed) {
                  this.setData({
                    canvasText: this.data.canvasText + "\n开启实时获取hrv：success"
                  })
                } else {
                  this.setData({
                    canvasText: this.data.canvasText + "\n开启实时获取hrv：fail"
                  })
                }
              })

              // 监听血压、ECG数据、PPG数据
              appControlAPI.receiveRealTimeData( (realTimeDataType, res) => {
                if (realTimeDataType == receivedRealTimeECG) {
                  util.printLog(JSON.stringify(res))
                  this.setData({
                    canvasText: this.data.canvasText + "\n实时ECG：" + JSON.stringify(res)
                  })
                } else if (realTimeDataType == receivedRealTimeBloodPressure){
                  // util.printLog(JSON.stringify(res))
                  this.setData({
                    canvasText: this.data.canvasText + "\n实时血压：" + JSON.stringify(res)
                  })
                } else if (realTimeDataType == receivedRealTimePPG) {
                  // util.printLog(JSON.stringify(res))
                  this.setData({
                    canvasText: this.data.canvasText + "\n实时PPG：" + JSON.stringify(res)
                  })
                } else if (realTimeDataType == measureECGStatus){
                  this.setData({
                    canvasText: this.data.canvasText + "\n测量ECG结束：" + JSON.stringify(res)
                  })
                } else if (realTimeDataType == receivedRealTimePressure){
                  this.setData({
                    canvasText: this.data.canvasText + "\n实时压力：" + JSON.stringify(res)
                  })
                } else if (realTimeDataType == electrodeKey) {
                  this.setData({
                    canvasText: this.data.canvasText + "\n佩戴状态：" + JSON.stringify(res)
                  })
                }
              })
              // 开启PPG上传
              appControlAPI.waveDataUploadControl(waveUploadStateUploadWithOutSerialnumber, waveUploadDataTypePPG, (state, res) => {
                if (state == succeed) {
                  this.setData({
                    canvasText: this.data.canvasText + "\n开启PPG上传：success"
                  })
                } else {
                  this.setData({
                    canvasText: this.data.canvasText + "\n开启PPG上传：fail"
                  })
                }
                alertUtil.closeToast()
              })
            } else {
              this.setData({
                canvasText: this.data.canvasText + "\n开启ECG上传：fail"
              })
            }
            alertUtil.closeToast()
          })
        }
    })

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})